package com.beilang.hotline.telrecord.page;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.context.ApplicationContext;
import static org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import java.io.IOException;
import java.io.File;
import java.util.List;
import java.util.Iterator;
import java.util.Date;
import java.util.Calendar;

import com.beilang.hotline.telattach.manager.TelattachMgr;
import com.beilang.hotline.telattach.model.TelattachModel;
import com.beilang.hotline.user.model.UserModel;
import com.beilang.hotline.telrecord.manager.TelrecordMgr;
import com.beilang.base.util.DateUtil;
import com.beilang.base.util.ReportUtil;
import flex.messaging.FlexContext;

public class FileUploadServlet extends HttpServlet {

    private int maxPostSize = 1024 * 1024 * 1024;

    public FileUploadServlet() {
        super();
    }

    public void destroy() {
        super.destroy();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String entityId= request.getParameter("entityId");
        String entityType= request.getParameter("entityType");
        String userId= request.getParameter("userId");
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(4096);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(maxPostSize);
        try {
            List fileItems = upload.parseRequest(request);
            Iterator iter = fileItems.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) {
                    try {
                       ServletContext context =this.getServletContext();
                       ApplicationContext ctx =getRequiredWebApplicationContext(context);
                       TelattachMgr mgr=(TelattachMgr)ctx.getBean("telattachMgr");
                       TelattachModel telattachModel = new TelattachModel();
                        telattachModel.setAttchContent(item.get());
                        ReportUtil reportUtil = new ReportUtil();
                        telattachModel.setAttchSize(reportUtil.getSize(telattachModel.getAttchContent().length));
                        telattachModel.setAttchName(item.getName());
                        telattachModel.setEntityId(new Long(entityId));
                        telattachModel.setEntityType(entityType);
                        telattachModel.setCreateId(userId);
                        telattachModel.setUpdateId(userId);
                        Date date = DateUtil.getDate();
                        telattachModel.setCreateTime(date);
                        telattachModel.setUpdateTime(date);
                        mgr.createTelattachModel(telattachModel) ;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
    }

   protected UserModel getLoginUser(HttpServletRequest request){
		return (UserModel) request.getSession().getAttribute("user");	
	}


    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
